﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TriPhoto
{
    /// <summary>
    /// Logique d'interaction pour ExifMetaData.xaml
    /// </summary>
    public partial class ExifMetaData : Window
    {
        private ImageExifMetadata exifMetaData = null;

        public ImageExifMetadata ImageExifMetadata
        {
            set
            {
                exifMetaData = value;
                updateUI();
            }
        }

        public ExifMetaData()
        {
            InitializeComponent();
        }

        private void updateUI()
        {
            if (exifMetaData == null)
                return;

            try
            {
                WithHeightLabel.Content = exifMetaData.Width.ToString() + " x " + exifMetaData.Height.ToString();
                DateLabel.Content = exifMetaData.DateImageTaken.ToString();
                CameraLabel.Content = exifMetaData.CameraModel.ToString();
                CreationSoftLabel.Content = exifMetaData.CreationSoftware.ToString();
                OuvertureLentilleLabel.Content = ConvertLensAperture(exifMetaData.LensAperture);
                DistanceFocalLabel.Content = ConvertFocalLength(exifMetaData.FocalLength);
                VitesseLabel.Content = exifMetaData.IsoSpeed.ToString();
                TempsExpositionLabel.Content = ConvertTimeExposure(exifMetaData.ExposureTime);
                ModeLabel.Content = ConvertExpositionMode(exifMetaData.ExposureMode);
                ExpositionCompensationLabel.Content = exifMetaData.ExposureCompensation.ToString();
                BalanceBlancLabel.Content = ConvertWhiteBlanc(exifMetaData.WhiteBalanceMode);
                ColorLabel.Content = ConvertColor(exifMetaData.ColorRepresentation);
            }
            catch (Exception e)
            { }
        }

        public string ConvertColor(ColorRepresentation in_color)
        {
            switch (in_color)
            { 
                case ColorRepresentation.sRGB:
                    return TriPhoto.Properties.Resources.sRGB;
                case ColorRepresentation.Uncalibrated:
                    return TriPhoto.Properties.Resources.Uncalibrated;
                default:
                    return TriPhoto.Properties.Resources.Unknown;
            }
        }

        public string ConvertTimeExposure(decimal? in_exposure)
        {
            try
            {
                if (in_exposure != null)
                {
                    decimal l_exposure = Decimal.Round(1 / in_exposure.Value);
                    return String.Format("1/{0} sec", l_exposure.ToString());
                }
                else
                    return null;
            }
            catch (NullReferenceException)
            {
                return null;
            }
        }

        public string ConvertWhiteBlanc(WhiteBalanceMode in_whiteBalance)
        {
            switch (in_whiteBalance)
            { 
                case WhiteBalanceMode.D55:
                    return TriPhoto.Properties.Resources.D55;
                case WhiteBalanceMode.D65:
                    return TriPhoto.Properties.Resources.D65;
                case WhiteBalanceMode.D75:
                    return TriPhoto.Properties.Resources.D75;
                case WhiteBalanceMode.Daylight:
                    return TriPhoto.Properties.Resources.Daylight;
                case WhiteBalanceMode.Flash:
                    return TriPhoto.Properties.Resources.Flash;
                case WhiteBalanceMode.Fluorescent:
                    return TriPhoto.Properties.Resources.Fluorescent;
                case WhiteBalanceMode.Other:
                    return TriPhoto.Properties.Resources.Other;
                case WhiteBalanceMode.StandardLightA:
                    return TriPhoto.Properties.Resources.StandardLightA;
                case WhiteBalanceMode.StandardLightB:
                    return TriPhoto.Properties.Resources.StandardLightB;
                case WhiteBalanceMode.StandardLightC:
                    return TriPhoto.Properties.Resources.StandardLightC;
                case WhiteBalanceMode.Tungsten:
                    return TriPhoto.Properties.Resources.Tungsten;
                case WhiteBalanceMode.Unknown:
                    return TriPhoto.Properties.Resources.Unknown;
                default:
                    return TriPhoto.Properties.Resources.Unknown;
            }
        }

        public string ConvertExpositionMode(ExposureMode in_exposureMode)
        {
            switch (in_exposureMode)
            {
                case ExposureMode.AperturePriority:
                    return TriPhoto.Properties.Resources.AperturePriority;

                case ExposureMode.HighSpeedMode:
                    return TriPhoto.Properties.Resources.HighSpeedMode;

                case ExposureMode.LandscapeMode:
                    return TriPhoto.Properties.Resources.LandscapeMode;

                case ExposureMode.LowSpeedMode:
                    return TriPhoto.Properties.Resources.LowSpeedMode;

                case ExposureMode.Manual:
                    return TriPhoto.Properties.Resources.Manual;

                case ExposureMode.NormalProgram:
                    return TriPhoto.Properties.Resources.NormalProgram;

                case ExposureMode.PortraitMode:
                    return TriPhoto.Properties.Resources.PortraitMode;

                case ExposureMode.ShutterPriority:
                    return TriPhoto.Properties.Resources.ShutterPriority;

                default:
                    return TriPhoto.Properties.Resources.Unknown;
            }
        }

        public string ConvertLensAperture(decimal? in_lensAperture)
        {
            if (in_lensAperture != null)
            {
                return String.Format("F{0:##.0}", in_lensAperture);
            }
            else
            {
                return String.Empty;
            }
        }

        public string ConvertFocalLength(decimal? in_focalLenth)
        {
            if (in_focalLenth != null)
            {
                return String.Format("{0}mm", in_focalLenth);
            }
            else
            {
                return String.Empty;
            }
        }
    }
}
